<!DOCTYPE html>
<html lang="en">

  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>练习六：对一个数组中的数字进行排序（冒泡排序/选择排序）</title>
  </head>

  <body>

  </body>
  <script>
    /* 
    练习六：对一个数组中的数字进行排序（冒泡排序/选择排序）
    解题思路：
        1.先找出一行中最小值
        2.循环起来
    */
    //冒泡排序 //从小到大排
    var arr = [10, 30, 25, 6, 8];
    var temp;
    /*
    //第一次：[10, 30, 25, 6, 8]; ->[10, 25, 6, 8,30]
    for (let index = 0; index < arr.length - 1; index++) { //4
      if (arr[index] >= arr[index + 1]) {
        temp = arr[index];
        arr[index] = arr[index + 1];
        arr[index + 1] = temp;
      }
    }
    //第二次：[10, 25, 6, 8,30]->[10,6, 8,25,30]
    for (let index = 0; index < arr.length - 1 - 1; index++) { //3
      if (arr[index] >= arr[index + 1]) {
        temp = arr[index];
        arr[index] = arr[index + 1];
        arr[index + 1] = temp;
      }
    }
    //第三次：[10,6, 8,25,30]->[6, 8,10,25,30]
    for (let index = 0; index < arr.length - 1 - 1 - 1; index++) { //2
      if (arr[index] >= arr[index + 1]) {
        temp = arr[index];
        arr[index] = arr[index + 1];
        arr[index + 1] = temp;
      }
    }
    //第四次：[6, 8,10,25,30]-》[6, 8,10,25,30]
    for (let index = 0; index < arr.length - 1 - 1 - 1 - 1; index++) { //1
      if (arr[index] >= arr[index + 1]) {
        temp = arr[index];
        arr[index] = arr[index + 1];
        arr[index + 1] = temp;
      }
    }

    */
    for (let j = 0; j < arr.length - 1; j++) { //外层循环起来
      for (let index = 0; index < arr.length - 1; index++) { //内层寻找最大值
        if (arr[index] >= arr[index + 1]) {
          temp = arr[index];
          arr[index] = arr[index + 1];
          arr[index + 1] = temp;
        }
      }
    }
    console.log(arr); //[6, 8,10,25,30]
  </script>

</html>